package org.fuys.coder.infrastructure.scheduled;

import org.fuys.coder.common.constants.RedisConstants;
import org.fuys.coder.infrastructure.util.RedisUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.scheduled
 * @className: UpdateSignUpRedisKey
 * @author: WangJing
 * @description: 每月更新Redis签到的Key
 * @date: 2024/6/30 17:17
 * @version: 1.0
 */

@Component
public class UpdateSignUpRedisKey {

    @Resource
    private RedisUtil redisUtil;

    @Scheduled(cron = "0 0 0 1 * ?")
    public void updateSignUpRedisKey(){
        //假设现在是7月
        int monthValue = LocalDateTime.now().getMonthValue();
        if(monthValue==12){
            monthValue=1;
        }else{
            monthValue+=1;
        }
        //下个月是8月 创建
        String newRedisKey= RedisConstants.REDIS_USE_SIGN_UP+monthValue;
        //上个月是6月 删除 因为数据没用了
        String oldRedisKey= RedisConstants.REDIS_USE_SIGN_UP+(monthValue-1);
        redisUtil.deleteKey(oldRedisKey);
        //提前创建8月的 同理 6月的是由5月创建的
        redisUtil.hashSet(newRedisKey,"user:0",0);
    }
}
